package com.gyg.controller;


import com.gyg.common.VO.BlogRemarkVO;
import com.gyg.common.VO.UserRemarkVO;
import com.gyg.common.dto.RemarkDTO;
import com.gyg.common.lang.Result;
import com.gyg.entity.Remark;
import com.gyg.service.IBlogService;
import com.gyg.service.IRemarkService;
import com.gyg.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 关注公众号：码猿编程日记
 * @since 2021-10-27
 */
@RestController
@RequestMapping("/remark")
public class RemarkController {

    @Autowired
    IRemarkService remarkService;

    @Autowired
    IUserService userService;

    @Autowired
    IBlogService blogService;

    /**
    * <desc>
    *   获列表
    * </desc>
    * @param
    * @return: com.gyg.common.lang.Result
    * @author LiuYu
    * @date: 2021-10-27
     **/
    @RequiresAuthentication
    @RequestMapping("/list")
    public Result getList(){
        List<Remark> list = remarkService.list();
        return Result.success(list);
    }

    /**
    * <desc>
    *   保存评论
    * </desc>
    * @param remarkDTO
    * @return: com.gyg.common.lang.Result
    * @author LiuYu
    * @date: 2021-10-27
     **/
    @RequiresAuthentication
    @RequestMapping("/save")
    public Result save(@RequestBody RemarkDTO remarkDTO){
        System.out.println(remarkDTO);
        Remark remark = new Remark();
        remark.setBlogId(remarkDTO.getBid()).setUserId(remarkDTO.getUid()).setRemark(remarkDTO.getR());
        remark.setCreated(LocalDateTime.now());
        remark.setStatus(0);
        remarkService.save(remark);
        return Result.success(true);
    }

    /**
    * <desc>
    *      获取用户评论表格内容
    * </desc>
    * @param
    * @return: com.gyg.common.lang.Result
    * @author LiuYu
    * @date: 2021-10-27
     **/
    @RequiresAuthentication
    @RequestMapping("/getUserRemarkList")
    public Result getUserRemarkList(){
        List<UserRemarkVO> list = remarkService.getUserRemarkList();
        return Result.success(list);
    }

    /**
    * <desc>
    *   更新评论状态
    * </desc>
    * @param id
    * @param status
    * @return: com.gyg.common.lang.Result
    * @author LiuYu
    * @date: 2021-10-27
     **/
    @RequiresAuthentication
    @RequestMapping("/status")
    public Result getUserRemarkList(@RequestParam("id") Integer id,@RequestParam("status") Integer status){
        remarkService.update(id,status);
        return Result.success(true);
    }


    /**
    * <desc>
    *   获取待审核评论数
    * </desc>
    * @param
    * @return: com.gyg.common.lang.Result
    * @author LiuYu
    * @date: 2021-10-27
     **/
    @RequiresAuthentication
    @RequestMapping("/getFailRemarkCount")
    public Result getFailRemarkCount(){
        Map<String,Integer> map = new HashMap<>();
        int failRemarkCount = remarkService.getFailRemarkCount();
        int totalRemarkCount = remarkService.list().size();
        map.put("failRemarkCount",failRemarkCount);
        map.put("totalRemarkCount",totalRemarkCount);
        return Result.success(map);
    }

    /**
    * <desc>
    *      获取文章用户评论信息
    * </desc>
    * @param blogId
    * @return: com.gyg.common.lang.Result
    * @author LiuYu
    * @date: 2021-10-27
     **/
    @RequiresAuthentication
    @RequestMapping("/getBlogRemarkList")
    public Result getBlogRemarkList(Integer blogId){

        Map<String,Object> map = new HashMap<>();
        List<BlogRemarkVO> list = remarkService.getBlogRemarkList(blogId);
        map.put("list",list);
        map.put("count",list.size());
        return Result.success(map);
    }

    /**
    * <desc>
    *   获取文章评论数
    * </desc>
    * @param blogId
    * @return: com.gyg.common.lang.Result
    * @author LiuYu
    * @date: 2021-10-27
     **/
    @RequiresAuthentication
    @RequestMapping("/getBlogRemarkListCount")
    public Result getBlogRemarkListCount(Integer blogId){

//        Integer count = remarkService.list(new QueryWrapper<Remark>().eq("blog_id",blogId).eq("status",1)).size();
//        return Result.success(count);
        return null;
    }
}
